package com.tongbo.mobilesafe.ui;

import java.io.File;

import com.tongbo.mobilesafe.R;
import com.tongbo.mobilesafe.engine.DownLoadFileTask;
import com.tongbo.mobilesafe.service.AddressService;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class AToolsActivity extends Activity implements OnClickListener{
	protected static final int ERROR = 10;
	protected static final int SUCCESS = 11;
	private TextView tv_query;
	private ProgressDialog pd;
	private TextView tv_atools_address;
	private TextView tv_atools_select_bg;
	private TextView tv_atools_change_location;
	private CheckBox cb_atools_address;
	private Intent serviceintent;
	private SharedPreferences sp;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case ERROR:
				Toast.makeText(getApplicationContext(), "下载数据库失败", 0).show();
				break;
			case SUCCESS:
				Toast.makeText(getApplicationContext(), "下载数据库成功", 0).show();
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.atools);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		tv_atools_select_bg = (TextView) this.findViewById(R.id.tv_atools_select_bg);
		tv_atools_change_location = (TextView) this.findViewById(R.id.tv_atools_change_location);
		serviceintent = new Intent(this,AddressService.class);
		tv_atools_address = (TextView) this.findViewById(R.id.tv_atools_address);
		cb_atools_address = (CheckBox) this.findViewById(R.id.cb_atools_address);
		cb_atools_address.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					startService(serviceintent);
					tv_atools_address.setTextColor(Color.WHITE);
					tv_atools_address.setText("号码归属地服务已经开启");
				}else{
					stopService(serviceintent);
					tv_atools_address.setTextColor(Color.RED);
					tv_atools_address.setText("号码归属地服务未开启");
				}
			}
		});
		tv_query = (TextView) this.findViewById(R.id.tv_atools_query);
		tv_query.setOnClickListener(this);
		tv_atools_select_bg.setOnClickListener(this);
		tv_atools_change_location.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_atools_query:
			//判断来电归属地数据库是否存在
			if(isDBExist()){
				Intent intent = new Intent(this,QueryNumberActivity.class);
				startActivity(intent);
			}else{
				//提示用户下载数据库
				pd = new ProgressDialog(this);
				pd.setMessage("正在下载数据库");
				pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				pd.show();
				//下载数据库
				new Thread(new Runnable() {
					@Override
					public void run() {
						String path = getResources().getString(R.string.addressdburl);
						String filePath = "/sdcard/address.db";
						try {
							DownLoadFileTask.getFile(path, filePath, pd);
							pd.dismiss();
							Message message = new Message();
							message.what = SUCCESS;
							handler.sendMessage(message);
						} catch (Exception e) {
							e.printStackTrace();
							pd.dismiss();
							Message message = new Message();
							message.what = ERROR;
							handler.sendMessage(message);
						}
					}
				}).start();
			}
			break;
		case R.id.tv_atools_select_bg://设定来电号码归属地的样式
			AlertDialog.Builder builder = new Builder(this);
			builder.setTitle("归属地提示显示风格");
			String[] items = new String[]{"半透明","活力橙","苹果绿"};
			builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Editor editor = sp.edit();
					editor.putInt("background", which);
					editor.commit();
				}
			});
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
				
			});
			builder.create().show();
			break;
		case R.id.tv_atools_change_location://设定来电号码归属地的显示位置
			Intent intent = new Intent(this,DragViewActivity.class);
			startActivity(intent);
			break;
		}
	}	
	/**
	 * 判断数据库是否存在
	 * @return
	 */
	public boolean isDBExist(){
		File file = new File("/sdcard/address.db");
		return file.exists();
	}
}